﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached.Base;
using ChannelVN.Cached.UrlRewrite;
using GUI.Extensions;
using ChannelVN.Cached;

namespace GUI.Pages.News
{
    public partial class ListNews : PageBase
    {
        #region Overrides of PageBase

        protected override string CacheConfigName
        {
            get { return "Pages-News-ListNews"; }
        }

        protected override bool IsCachedChanged(long newsId, string zoneId, long tagId)
        {
            return !string.IsNullOrEmpty(zoneId);
        }

        #endregion

        private const int TopHighlightNews = 3;
        private const int MaxPage = 1;
        private const int PageSize = 15;
        private const int NumberOfPageLoadFromNewsPosition = 2;
        public int ZoneId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            var objCategory = RequestExtensions.Category;
            if (objCategory != null)
            {
                ZoneId = objCategory.Id;
                var isCachedUpdating = CacheController.IsCachedUpdating;

                ctrEmbedThreads.ZoneId = objCategory.ParentId > 0 ? objCategory.ParentId : objCategory.Id;
                var bo = CacheSettingExtensions.EnableCacheHtml ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();

                var containsPageIndex = RequestExtensions.ContainsPageIndex;
                var pageIndex = RequestExtensions.PageIndex;
                var isAjax = RequestExtensions.IsAjax;

                #region Tin nổi bật
                {
                    var dt = bo.GetNewsByPosition(
                    top: TopHighlightNews,
                    typeId: objCategory.ParentId > 0 ? DefinedConstants.News.TypeIDs.TinTrangChuyenMuc : DefinedConstants.News.TypeIDs.NoiBatChuyenMuc,
                    zoneId: objCategory.Id,
                    getDataFromMasterDataBase: isCachedUpdating);

                    ctrHighlightNews.Visible = ctrHighlightNews.DataBind(dt);
                }
                #endregion

                #region ListNews
                {
                    DataTable dt;
                    if (isAjax)
                    {
                        dt = bo.GetNewsByPosition(
                        top: MaxPage * PageSize,
                        topExclusion: objCategory.ParentId > 0 ? TopHighlightNews : 0,
                        typeId: DefinedConstants.News.TypeIDs.TinTrangChuyenMuc,
                        zoneId: objCategory.Id,
                        getDataFromMasterDataBase: isCachedUpdating);
                    }
                    else
                    {
                        dt = pageIndex <= NumberOfPageLoadFromNewsPosition
                            ? bo.GetNewsByPosition(top: PageSize, typeId: DefinedConstants.News.TypeIDs.TinTrangChuyenMuc, topExclusion: (pageIndex - 1) * PageSize + (objCategory.ParentId > 0 ? TopHighlightNews : 0), zoneId: objCategory.Id, getDataFromMasterDataBase: isCachedUpdating)
                            : bo.GetListNews(pageIndex: pageIndex, pageSize: PageSize, topExclusion: objCategory.ParentId > 0 ? TopHighlightNews : 0, zoneId: objCategory.Id);
                    }
                    ctrListNews.Visible = ctrListNews.DataBind(dt);

                    ctrPaging.SumPage = NewsBO.GetInstance().GetTotalPagesOfListNews(pageSize: PageSize, zoneId: objCategory.Id);
                    ctrPaging.LinkSite = RequestExtensions.Url;
                    ctrPaging.Padding(isAjax ? MaxPage : pageIndex);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        var r = dt.Rows[0];
                        ctrGoogleSnippet.Avatar = PresentationUtils.Photo.GetSourceImage(r[SqlConstants.NewsPublish.Avatar].ToString());

                    }
                }
                #endregion

                #region Box ảnh đẹp hiển thị ở mục giải trí và đẹp
                {
                    //if (objCategory.Id == DefinedConstants.Zone.GiaiTri ||
                    //   objCategory.Id == DefinedConstants.Zone.Dep)
                    //{
                    //    phBoxPhoto.Controls.Add(LoadControl("/Controls/Photo/BoxPhotoEmbed.ascx"));
                    //}
                }
                #endregion

                #region OtherNews
                {
                    //DataTable dt;
                    //var currentPageIndex = isAjax ? MaxPage : pageIndex;
                    //if (currentPageIndex < NumberOfPageLoadFromNewsPosition)
                    //{
                    //    dt = EnableCacheHtml
                    //             ? NewsBO_NoCache.GetInstance().GetNewsByPosition(top: PageSize, topExclusion: pageIndex * PageSize, typeId: DefinedConstants.News.TypeIDs.TinTrangChuyenMuc, zoneId: objCategory.Id)
                    //             : NewsBO.GetInstance().GetNewsByPosition(top: PageSize, topExclusion: pageIndex * PageSize, typeId: DefinedConstants.News.TypeIDs.TinTrangChuyenMuc, zoneId: objCategory.Id);
                    //}
                    //else
                    //{
                    //    dt = EnableCacheHtml
                    //        ? NewsBO_NoCache.GetInstance().GetListNews(pageIndex: isAjax ? MaxPage + 1 : pageIndex + 1, pageSize: PageSize, zoneId: objCategory.Id)
                    //        : NewsBO.GetInstance().GetListNews(pageIndex: isAjax ? MaxPage + 1 : pageIndex + 1, pageSize: PageSize, zoneId: objCategory.Id);
                    //}
                    //divOtherNews.Visible = ctrOtherNews.DataBind(dt);
                }
                #endregion

                var mtaCategory = NewsBO.GetInstance().GetCategoryMetaData(objCategory.Id);
                var mtaParentCategory = objCategory.ParentId > 0
                                            ? NewsBO.GetInstance().GetCategoryMetaData(objCategory.ParentId)
                                            : mtaCategory;
                #region Css/Js
                {
                    if (mtaParentCategory != null && !string.IsNullOrEmpty(mtaParentCategory.ThemeUrl))
                    {
                        PresentationUtils.AddCss(Page, mtaParentCategory.ThemeUrl, usingMasterPageVersion: true, isTheme: true);
                    }
                }
                #endregion

                #region Title/MetaData
                if (mtaCategory != null)
                {
                    var extraText = (containsPageIndex ? string.Format(" | page {0}", pageIndex) : string.Empty);
                    PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.Description.Id,
                                              DefinedConstants.MetaAttributes.Description.Name, mtaCategory.Description + extraText);
                    PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.Keywords.Id,
                                              DefinedConstants.MetaAttributes.Keywords.Name, mtaCategory.Keywords);
                    PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.NewsKeywords.Id,
                                              DefinedConstants.MetaAttributes.NewsKeywords.Name, mtaCategory.Keywords);

                    Page.Header.Title = string.Format("{0} | soha.vn{1}", mtaCategory.Title, extraText);

                    ctrGoogleSnippet.Url = PresentationUtils.UrlBuilder.BuildAbsoluteUrl(objCategory.Url);
                    ctrGoogleSnippet.Title = mtaCategory.Title;
                    ctrGoogleSnippet.Description = mtaCategory.Description;
                }
                #endregion

                return;
            }

            _301Redirection.RedirectToFileNotFoundPage(RequestExtensions.CatUrl);

        }
    }
}